/*
 *  Cero Project - Copyright   2006 The Cero Developement Team 
 *  (Michael Laguerre, Camille Roux, Matthieu Segret, Mathieu Sivade) 
 * 
 *  This program is free software; you can redistribute it and/or modify it 
 *  under the terms of the GNU General Public License as published by the Free 
 *  Software Foundation; either version 2 of the License, or (at your option) 
 *  any later version.
 * 
 *  This program is distributed in the hope that it will be useful, but 
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 *  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 *  for more details.
 */

package org.ceroproject.games.base;

import java.util.Collection;

import org.ceroproject.games.Action;
import org.ceroproject.games.ActionEvent;
import org.ceroproject.games.ActionException;
import org.ceroproject.games.Card;
import org.ceroproject.games.Game;
import org.ceroproject.games.Player;
import org.ceroproject.games.Zone;


/**
 * Base class for actions. It provides the basic comportment actions should
 * have. Inherit from it and override only the functions you want. That's just
 * about it ;)
 * 
 * @author Telem
 */
public abstract class ActionBase implements Action {
	private String name;

	private ActionEvent event;

	public ActionBase(String name) {
		this.name = name;
	}

	public String getActionName() {
		return name;
	}

	/**
	 * @inheritDoc
	 */
	public void playerActed(ActionEvent event) throws ActionException {
		this.event = event;
		if (!canAct(event))
			throw new ActionException(
					"This action can't be played that way. Action : "
							+ this.getClass().getName() + ", event : "
							+ event.toString());
		if (event.getCards() != null && !event.getCards().isEmpty()) {
			if (event.getZoneFrom() == null)
				throw new ActionException("Cards must come from a zone");
			else if (!event.getZoneFrom().getSortedList().containsAll(
					event.getCards()))
				throw new ActionException(
						"Cards must come from the \"from\" zone");
		}
	}

	public ActionEvent getEvent() {
		return event;
	}

	public String toString() {
		return getActionName();
	}

	/**
	 * Returns the cards the action is using
	 * 
	 * @return the cards the action is using
	 */
	public Collection<Card> getCards() {
		return getEvent().getCards();
	}

	/**
	 * Returns the game the action is using
	 * 
	 * @return the game the action is using
	 */
	public Game getGame() {
		return getEvent().getGame();
	}

	/**
	 * Returns the player concerned by the action
	 * 
	 * @return the player concerned by the action
	 */
	public Player getPlayer() {
		return getEvent().getPlayer();
	}

	/**
	 * Returns the zone where the action started
	 * 
	 * @return the zone where the action started
	 */
	public Zone getZoneFrom() {
		return getEvent().getZoneFrom();
	}

	/**
	 * Returns the zone where the action ended
	 * 
	 * @return the zone where the action ended
	 */
	public Zone getZoneTo() {
		return getEvent().getZoneTo();
	}

}
